package org.jeecg.modules.job;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.util.TypeUtils;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.c.entity.DSignalH;
import org.jeecg.modules.c.entity.MSC;
import org.jeecg.modules.c.entity.MSignal;
import org.jeecg.modules.c.service.IDSignalHService;
import org.jeecg.modules.c.service.IMSCService;
import org.jeecg.modules.c.service.IMSignalService;
import org.jeecg.modules.client.ChannelManager;
import org.jeecg.modules.client.NettyClient;
import org.jeecg.modules.concat.FocConcat;
import org.jeecg.modules.enmu.AttributeLength;
import org.jeecg.modules.hand.LoginHandler;
import org.jeecg.modules.utils.SendMessageUtils;
import org.jeecg.modules.vo.Message;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.ByteBuffer;
import java.util.Date;
import java.util.List;

import static org.jeecg.modules.enmu.MessageTypeEnum.DYN_ACCESC_MODE_ACK;

/**
 * 实时数据上传任务
 */

@Component
@Slf4j
public class DataUploadJob implements Job {




    @Autowired
    private IMSignalService mSignalService;
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        //从通道管理对象中获取通道列表
       if( LoginHandler.goladChannel==null){
           log.info("通道暂未链接");
           return;
       }
        log.info("开始发送性能数据");
        Channel channel = LoginHandler.goladChannel;
        //获取现在要发送的信号数据
        List<MSignal> signalList = mSignalService.getSendData();
        //开始组装信号发送数据
        //全部处理完毕后，发送数据
        Message message = new Message();
        message.setMessageType(DYN_ACCESC_MODE_ACK.getValue());
        message.setMessageSerialNo(SendMessageUtils.messageSerialNo);
        //构造发送字节
        ByteBuf content =   Unpooled.buffer();

        //设置scid
        byte[] scBytes = SendMessageUtils.padWithSpaces(FocConcat.SCID, AttributeLength.SCID_LEN.getLength());

        content.writeBytes(scBytes);
        //设置发送groupid
        //生成随机数，保持在int范围内，并且是正数
        int groupId = (int) (Math.random() * 100000000);
        content.writeIntLE(groupId);
        if (CollectionUtil.isEmpty(signalList)) {
            content.writeIntLE(0);
            //没有发送数据，直接返回
            return;
        } else {
            content.writeIntLE(1);
        }
        //数据数量
        content.writeIntLE(signalList.size());
        //获取到性能数据列表
        log.info("发送数据：" + JSONObject.toJSONString(signalList));
        //如果是空则直接返回
        if (CollectionUtil.isEmpty(signalList)) {
            return;
        }
        //TA/TD
        for (MSignal mSignal : signalList) {
            mSignal.setUploadtime(new Date());
            //数据类型
            content.writeIntLE(mSignal.getType());

            byte[] bytes = SendMessageUtils.padWithSpaces(mSignal.getId(), AttributeLength.ID_LEN.getLength());
            //数据标识号
            content.writeBytes(bytes);
            //数据值
            if (FocConcat.Analog.contains(mSignal.getType())) {
                if (mSignal.getValue() == null) {
                    content.writeFloatLE(0);
                    continue;
                }else {
                    content.writeFloatLE(TypeUtils.castToFloat(mSignal.getValue()));
                }
            } else {
                if (mSignal.getValue() == null) {
                    content.writeIntLE(0);
                    continue;
                }else {
                    content.writeIntLE(TypeUtils.castToInt(mSignal.getValue()));
                }

            }
            //数据状态
            content.writeIntLE(0);
        }
        //无效健康点数量
        content.writeIntLE(0);

        message.setBuffer(content);
        channel.writeAndFlush(message);
        mSignalService.updateBatchById(signalList);
    }


}
